﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.OleDb;


public partial class IzbrisiNaracka : System.Web.UI.Page
{
    int index = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        string conString = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
        OleDbConnection conn = new OleDbConnection(conString);
        conn.Open();
        string select = "select ID from Poracki";
        OleDbCommand cmd = new OleDbCommand(select, conn);
        OleDbDataReader citanka = cmd.ExecuteReader();
        ListItem item = new ListItem();

        while (citanka.Read())
        {
            CheckBoxList1.Items.Add(item.Value = citanka.GetValue(0).ToString());
            index++;
        }
        conn.Close();

    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        string conString = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
        OleDbConnection conn = new OleDbConnection(conString);
        conn.Open();

        int[] chekirani = new int[index];

        for (int i = 0; i < index; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
                chekirani[i] = 1;
            else
                chekirani[i] = 0;
        }

        for (int i = 0; i < index; i++)
        {
            if (chekirani[i] == 1)
            {
                string ID = "";
                int broj = 0;
                string select = "select ID from Poracki where Tip_na_naracka=?";
                OleDbCommand cmd = new OleDbCommand(select, conn);
                cmd.Parameters.AddWithValue("?", CheckBoxList1.Items[i].Text.ToString());
                OleDbDataReader citanka = cmd.ExecuteReader();
                citanka.Read();
                ID = citanka.GetValue(0).ToString();
                string selectall = "select ID from Poracki";
                cmd = new OleDbCommand(selectall, conn);
                citanka = cmd.ExecuteReader();
                while (citanka.Read())
                {
                    if (ID.Equals(citanka.GetValue(0).ToString()))
                        broj++;
                }
                string brisi = "delete from Poracki where ID=?";
                cmd = new OleDbCommand(brisi, conn);
                cmd.Parameters.AddWithValue("?", CheckBoxList1.Items[i].Text.ToString());
                cmd.ExecuteNonQuery();

                if (broj < 2)
                {
                    brisi = "delete from Poracki where ID=?";
                    cmd = new OleDbCommand(brisi, conn);
                    cmd.Parameters.AddWithValue("?", ID);
                    cmd.ExecuteNonQuery();
                }

            }
        }
        conn.Close();
        Response.Redirect("Naracki.aspx");

    }
}